﻿using Dto;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace ZY17.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HealthController : ControllerBase
    {
        public HealthController(IHeathBLL bll)
        {
            Bll = bll;
        }

        public IHeathBLL Bll { get; }
        [HttpPost]
        public Task<int> AddHeath(HealthModel health)
        {
            return Bll.AddHeath(health);
        }
        [HttpGet]
        public Task<int> DeleteRangeHealth(string idstring)
        {
            return Bll.DeleteRangeHealth(idstring);
        }
        [HttpGet]
        public HealthShowDto HealthShow(string? name, int pageindex, int pagesize)
        {
           return Bll.HealthShow(name, pageindex, pagesize);
        }
        [HttpGet]
        public Task<int> UpdateRangeState(string idstring)
        {
            return Bll.UpdateRangeState(idstring);
        }
        [HttpGet]
        public List<HealthModel> GetHealthList()
        {
            return Bll.GetHealthList();
        }
        [HttpGet]
        public IActionResult Dc() 
        {
            List<HealthModel>health=Bll.GetHealthList();
            XSSFWorkbook sheets=new XSSFWorkbook();
            var sheet=sheets.CreateSheet();
            var row=sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("Id");
            row.CreateCell(1).SetCellValue("项目名称");
            row.CreateCell(2).SetCellValue("编号");
            row.CreateCell(3).SetCellValue("佣金");
            row.CreateCell(4).SetCellValue("人数");
            row.CreateCell(5).SetCellValue("时长");
            row.CreateCell(6).SetCellValue("项目说明");
            row.CreateCell(7).SetCellValue("状态");
            row.CreateCell(8).SetCellValue("是否删除");
            row.CreateCell(9).SetCellValue("创建人Id");
            row.CreateCell(10).SetCellValue("创建日期");
            row.CreateCell(11).SetCellValue("修改人Id");
            row.CreateCell(12).SetCellValue("修改日期");
            row.CreateCell(13).SetCellValue("删除人Id");
            row.CreateCell(14).SetCellValue("删除日期");

            int num = 1;
            foreach (var item in health)
            {
                var r = sheet.CreateRow(num);
                r.CreateCell(0).SetCellValue(item.Id);
                r.CreateCell(1).SetCellValue(item.HelthName);
                r.CreateCell(2).SetCellValue(item.Bh);
                r.CreateCell(3).SetCellValue(item.Money);
                r.CreateCell(4).SetCellValue(item.People);
                r.CreateCell(5).SetCellValue(item.Hour);
                r.CreateCell(6).SetCellValue(item.Xm);
                r.CreateCell(7).SetCellValue(item.State==1 ?"启用":"禁用");
                r.CreateCell(8).SetCellValue(item.IsDelete);
                r.CreateCell(9).SetCellValue(item.CreateId);
                r.CreateCell(10).SetCellValue(item.CreateTime==null?"":item.CreateTime.ToString("yyyy-MM-dd HH:mm:dd"));
                r.CreateCell(11).SetCellValue(item.UpdateId);
                r.CreateCell(12).SetCellValue(item.UpdateDate==null?"":item.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:dd"));
                r.CreateCell(13).SetCellValue(item.DeleteId);
                r.CreateCell(14).SetCellValue(item.DeleteDate == null ? "" : item.DeleteDate.Value.ToString("yyyy-MM-dd HH:mm:dd"));
                num++;
            }
            MemoryStream stream=new MemoryStream();
            sheets.Write(stream);
            byte[]data=stream.ToArray();



            string mine = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(data, mine, "健康理疗.xlsx");
        }
    }
}
